package com.sgl.gk.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sg.comm.common.Ret;
import com.sg.comm.opcuav2.NodeDTOV2;
import com.sg.comm.opcuav2.OpcUaReadReqV2;
import com.sg.comm.opcuav2.OpcUaWriteReqV2;
import com.sg.comm.opcuav2.batch.BatchReadDTO;
import com.sg.comm.opcuav2.single.ReadByNodeReq;
import com.sg.comm.service.OpcUaServiceV2;
import com.sgl.common.core.controller.BaseController;
import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.utils.ServletUtils;
import com.sgl.common.utils.StringUtils;
import com.sgl.common.utils.poi.ExcelUtil;
import com.sgl.gk.controller.vo.*;
import com.sgl.gk.dict.StationDict;
import com.sgl.gk.domain.*;
import com.sgl.gk.domain.plc.Node;
import com.sgl.gk.domain.plc.OpcUa;
import com.sgl.gk.domain.plc.OpcUaList;
import com.sgl.gk.mapper.*;
import com.sgl.gk.scheduled.ScheduledTask;
import com.sgl.gk.scheduled.vo.ServiceData;
import com.sgl.gk.scheduled.vo.SocketDataVo;
import com.sgl.gk.service.GkSettingLogService;
import com.sgl.gk.service.GkStationConfigurationService;
import com.sgl.gk.tools.BarCodeTool;
import com.sgl.gk.tools.OpcUATool;
import com.sgl.gk.tools.ReadOpcUaNodeInfoTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
@Slf4j
public class GkStationConfigurationServiceImpl extends BaseController implements GkStationConfigurationService {

    @Autowired
    GkStationConfigurationMapper gkStationConfigurationMapper;

    @Autowired
    GkStationConfigurationValuesMapper gkStationConfigurationValuesMapper;

    @Autowired
    GkStationEventMapper gkStationEventMapper;

    @Autowired
    GkStationMapper gkStationMapper;

    @Autowired
    GkProductMapper gkProductMapper;

    @Autowired
    GkProductionLineMapper gkProductionLineMapper;

    @Autowired
    GkSparePartsMapper gkSparePartsMapper;

    @Autowired
    GkStationConfigurationConnectMapper gkStationConfigurationConnectMapper;

    @Autowired
    GkSignalCommunicationMapper gkSignalCommunicationMapper;

    @Autowired
    GkStationConfigureTemplateInfoMapper gkStationConfigureTemplateInfoMapper;

    @Autowired
    GkSettingLogService gkSettingLogService;

    @Autowired
    GkReservedInfoMapper gkReservedInfoMapper;

    @DubboReference(version = "1.0.0", url = "dubbo://127.0.0.1:20880", check = false, timeout = 15000)
    private OpcUaServiceV2 opcUaServiceV2;

    @Value("${communication.mode}")
    public String communication;

    @Override
    public List<GkStationConfigurationVo> list(GkStationConfiguration gkStationConfiguration) {
        List<GkStationConfigurationVo> list = gkStationConfigurationMapper.list(gkStationConfiguration);
        for (GkStationConfigurationVo gkStationConfigurationVo : list) {
            gkStationConfigurationVo.setDefaultValue(gkStationConfigurationVo.getDefaultValue() == null ? gkStationConfigurationVo.getDefaultValue() : (gkStationConfigurationVo.getDataType().equals("boolean") ? gkStationConfigurationVo.getDefaultValue().equals("false") ? Boolean.FALSE : Boolean.TRUE : gkStationConfigurationVo.getDefaultValue()));
        }
        Long stationId = gkStationConfiguration.getStationId();
        Integer type = gkStationConfiguration.getType();
        GkStationConfigurationConnect gkStationConfigurationConnect = new GkStationConfigurationConnect();
        gkStationConfigurationConnect.setStationId(stationId);
        List<GkStationConfigurationConnect> configurationConnects = gkStationConfigurationConnectMapper.selectByStationIdAndType(gkStationConfigurationConnect);
        if (configurationConnects.size() > 0) {
            Long id = configurationConnects.get(0).getId();
            List<GkStationConfigurationValues> gkStationConfigurationValues = gkStationConfigurationValuesMapper.selectByStationConfigurationConnectId(id);
            for (GkStationConfigurationVo gkStationConfigurationVo : list) {
                for (GkStationConfigurationValues gkStationConfigurationValue : gkStationConfigurationValues) {
                    if (gkStationConfigurationValue.getStationConfigurationId().equals(gkStationConfigurationVo.getId())) {
                        gkStationConfigurationVo.setSaveValue(gkStationConfigurationValue.getValue());
                    }
                }
            }
        }
        List<GkStation> gkStations = gkStationMapper.selectInfosByIds(Collections.singletonList(gkStationConfiguration.getStationId()));
        if (gkStations != null || gkStations.size() > 0) {
            List<Long> signalIds = gkStations.stream().map(GkStation::getSignalCommunicationId).collect(Collectors.toList());
            List<GkSignalCommunication> gkSignalCommunicationList = gkSignalCommunicationMapper.selectInfoByIds(signalIds);
            OpcUaReadReqV2 opcUaReadReqV2 = new OpcUaReadReqV2();
            for (GkSignalCommunication gkSignalCommunication : gkSignalCommunicationList) {
                if (gkSignalCommunication.getCommunicationProtocol() != null) {
                    opcUaReadReqV2.setUid(gkSignalCommunication.getCommunicationProtocol());
                }
            }
            List<NodeDTOV2> data = new ArrayList<>();
            List<GkProductionLineVo> gkProductionLineVos = gkProductionLineMapper.selectInfoByIds(gkStations.stream().map(GkStation::getProductionLineId).collect(Collectors.toList()));
            if (communication.equals("plc")) {
                BatchReadDTO data1 = null;
                try {
                    ReadByNodeReq readByNodeReq = new ReadByNodeReq();
                    readByNodeReq.setUid(opcUaReadReqV2.getUid());
                    List<NodeDTOV2> nodeDTOV2List = new ArrayList<>();
                    nodeDTOV2List = GetNodeDTO2List(list,null,gkStations,gkProductionLineVos,readByNodeReq.getUid());
                    readByNodeReq.setNodeDTOV2List(nodeDTOV2List);
                    Ret<BatchReadDTO> batchReadDTORet = opcUaServiceV2.readDataByNodeList(readByNodeReq);
                    data1 = batchReadDTORet.getData();
//                    data1 = opcUaServiceV2.readData(opcUaReadReqV2).getData();
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
                if (data1 != null && data1.getIsConn()) {
                    data = data1.getDtoList();
                }
            } else if (communication.equals("socket")) {
                BufferedWriter bufferedWriter = ScheduledTask.bufferedWriter;
                BufferedReader bufferedReader = ScheduledTask.bufferedReader;
                try {
                    bufferedWriter.write(opcUaReadReqV2.getUid() + "\n");
                    bufferedWriter.flush();
                    String s = bufferedReader.readLine();
                    if (!s.equals("NO")) {
                        List<SocketDataVo> socketDataVos = new ArrayList<>();
                        JSONArray objects = JSON.parseArray(s);
                        for (Object object : objects) {
                            SocketDataVo socketDataVo = JSON.parseObject(object.toString(), SocketDataVo.class);
                            socketDataVos.add(socketDataVo);
                        }
                        for (SocketDataVo socketDataVo : socketDataVos) {
                            List<ServiceData> nodeList = socketDataVo.getNodeList();
                            for (ServiceData serviceData : nodeList) {
                                NodeDTOV2 nodeDTOV2 = new NodeDTOV2();
                                nodeDTOV2.setNodeValue(serviceData.getNodeValue());
                                nodeDTOV2.setNodeType(serviceData.getNodeType());
                                nodeDTOV2.setNodeName(serviceData.getNodeName());
                                nodeDTOV2.setNodeId(serviceData.getNodeId());
                                nodeDTOV2.setDisplayName(serviceData.getDisplayName());
                                nodeDTOV2.setStatusCode(0l);
                                data.add(nodeDTOV2);
                            }
                        }
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            for (GkStationConfigurationVo gkStationConfigurationVo : list) {
                for (NodeDTOV2 datum : data) {
                    Object obj = new Object();
                    if (datum.getNodeValue() != null) {
                        int i = datum.getNodeValue().toString().replace("}", "").indexOf("=");
                        if (i > 0) {
                            obj = datum.getNodeValue().toString().replace("}", "").substring(i + 1);
                        } else {
                            obj = datum.getNodeValue();
                        }
                    } else {
                        obj = datum.getNodeValue();
                    }
                    GkStation gkStation1 = new GkStation();
                    for (GkStation gkStation : gkStations) {
                        gkStation1 = gkStation;
                    }
                    String nodeName = new String();
                    if (gkStationConfigurationVo.getType() == 1) {
                        nodeName = "DB_RECIPE." + ((gkStation1.getCode() == null || gkStation1.getCode().equals("")) ? "" : (gkStation1.getCode() + ".")) + gkStationConfigurationVo.getVariableName();
                    }
                    if (gkStationConfigurationVo.getType() == 2) {
                        for (GkProductionLineVo gkProductionLineVo : gkProductionLineVos) {
                            if (gkStation1.getProductionLineId().longValue() == gkProductionLineVo.getId().longValue()) {
                                if (!StringUtils.isEmpty(gkStation1.getDataSource())) {
                                    nodeName = gkStation1.getDataSource() + "." + ((gkStation1.getCode() == null || gkStation1.getCode().equals("")) ? "" : (gkStation1.getCode() + ".")) + gkStationConfigurationVo.getVariableName();
                                }else {
                                    nodeName = gkProductionLineVo.getCode() + "." + ((gkStation1.getCode() == null || gkStation1.getCode().equals("")) ? "" : (gkStation1.getCode() + ".")) + gkStationConfigurationVo.getVariableName();
                                }
                            }
                        }
                    }
                    if (gkStationConfigurationVo.getType() == 3) {
                        // 写入交互变量类型  3写
                        nodeName = "data_pc_plc.pc_reply." + gkStationConfigurationVo.getVariableName();
                    }
                    if (gkStationConfigurationVo.getType() == 4) {
                        // 读取交互变量类型  4读
                        nodeName = "data_pc_plc.plc_request." + gkStationConfigurationVo.getVariableName();
                    }
                    if (datum.getDisplayName().equals(nodeName.trim())) {
                        if (gkStationConfigurationVo.getResultConversion()==1){
                            String s = "";
                            if (obj != null) {
                                s = obj.toString();
                            }
                            String value = "";
                            switch (s) {
                                case "0":;
                                case "0.0":value = gkStationConfigurationVo.getZeroValue();break;
                                case "1":
                                case "1.0":value = gkStationConfigurationVo.getOneValue();break;
                                case "2":
                                case "2.0":value = gkStationConfigurationVo.getTwoValue();break;
                                case "3":
                                case "3.0":value = gkStationConfigurationVo.getThreeValue();break;
                                case "4":
                                case "4.0":value = gkStationConfigurationVo.getFourValue();break;
                                case "5":
                                case "5.0":value = gkStationConfigurationVo.getFiveValue();break;
                            }
                            if (!StringUtils.isEmpty(value)){
                                gkStationConfigurationVo.setShowValue(value);
                            }else {
                                gkStationConfigurationVo.setShowValue(obj);
                            }
                        }else {
                            gkStationConfigurationVo.setShowValue(obj);
                        }
                    }
                }
            }
        }
        return list;
    }

    private List<NodeDTOV2> GetNodeDTO2List(List<GkStationConfigurationVo> list,List<GkStationConfiguration> gkStationConfigurationList, List<GkStation> gkStations,List<GkProductionLineVo> gkProductionLineVos,String uId){
        List<NodeDTOV2> nodeDTOV2List = new ArrayList<>();
        try {
            OpcUaList opcUaList = ReadOpcUaNodeInfoTool.GetOpcUaList();
            List<OpcUa> uaList = opcUaList.getOpcUaList();
            List<Node> fileNodeList = new ArrayList<>();
            for (OpcUa opcUa : uaList) {
                if (opcUa.getUid().equals(uId)) {
                    fileNodeList = opcUa.getNodeList();
                }
            }
            if (CollUtil.isNotEmpty(list)) {
                for (GkStationConfigurationVo gkStationConfigurationVo : list) {
                    NodeDTOV2 nodeDTOV2 = new NodeDTOV2();
                    GkStation gkStation1 = new GkStation();
                    for (GkStation gkStation : gkStations) {
                        gkStation1 = gkStation;
                    }
                    String nodeName = new String();
                    if (gkStationConfigurationVo.getType() == 1) {
                        nodeName = "DB_RECIPE." + ((gkStation1.getCode() == null || gkStation1.getCode().equals("")) ? "" : (gkStation1.getCode() + ".")) + gkStationConfigurationVo.getVariableName();
                    }
                    if (gkStationConfigurationVo.getType() == 2) {
                        for (GkProductionLineVo gkProductionLineVo : gkProductionLineVos) {
                            if (gkStation1.getProductionLineId().longValue() == gkProductionLineVo.getId().longValue()) {
                                if (!StringUtils.isEmpty(gkStation1.getDataSource())) {
                                    nodeName = gkStation1.getDataSource() + "." + ((gkStation1.getCode() == null || gkStation1.getCode().equals("")) ? "" : (gkStation1.getCode() + ".")) + gkStationConfigurationVo.getVariableName();
                                } else {
                                    nodeName = gkProductionLineVo.getCode() + "." + ((gkStation1.getCode() == null || gkStation1.getCode().equals("")) ? "" : (gkStation1.getCode() + ".")) + gkStationConfigurationVo.getVariableName();
                                }
                            }
                        }
                    }
                    if (CollUtil.isNotEmpty(fileNodeList)) {
                        for (Node node : fileNodeList) {
                            if (node.getDisplayName().equals(nodeName)) {
                                nodeDTOV2.setNodeId(node.getNodeId());
                                nodeDTOV2.setNodeType(node.getNodeType());
                                nodeDTOV2.setDisplayName(nodeName);
                                nodeDTOV2.setNodeName(node.getNodeName());
                                nodeDTOV2List.add(nodeDTOV2);
                                break;
                            }
                        }
                    }
                }
            } else if (CollUtil.isNotEmpty(gkStationConfigurationList)) {
                Map<Long, List<GkStationConfiguration>> stationConfigMap = gkStationConfigurationList.parallelStream().collect(Collectors.groupingBy(GkStationConfiguration::getStationId));
                for (GkStation gkStation : gkStations) {
                    List<GkStationConfiguration> gkStationConfigurations = stationConfigMap.get(gkStation.getId().longValue());
                    if (CollUtil.isNotEmpty(gkStationConfigurations)) {
                        for (GkStationConfiguration gkStationConfigurationVo : gkStationConfigurations) {
                            NodeDTOV2 nodeDTOV2 = new NodeDTOV2();
                            GkStation gkStation1 = gkStation;
                            String nodeName = new String();
                            if (gkStationConfigurationVo.getType() == 1) {
                                nodeName = "DB_RECIPE." + ((gkStation1.getCode() == null || gkStation1.getCode().equals("")) ? "" : (gkStation1.getCode() + ".")) + gkStationConfigurationVo.getVariableName();
                            }
                            if (gkStationConfigurationVo.getType() == 2) {
                                for (GkProductionLineVo gkProductionLineVo : gkProductionLineVos) {
                                    if (gkStation1.getProductionLineId().longValue() == gkProductionLineVo.getId().longValue()) {
                                        if (!StringUtils.isEmpty(gkStation1.getDataSource())) {
                                            nodeName = gkStation1.getDataSource() + "." + ((gkStation1.getCode() == null || gkStation1.getCode().equals("")) ? "" : (gkStation1.getCode() + ".")) + gkStationConfigurationVo.getVariableName();
                                        } else {
                                            nodeName = gkProductionLineVo.getCode() + "." + ((gkStation1.getCode() == null || gkStation1.getCode().equals("")) ? "" : (gkStation1.getCode() + ".")) + gkStationConfigurationVo.getVariableName();
                                        }
                                    }
                                }
                            }
                            if (CollUtil.isNotEmpty(fileNodeList)) {
                                for (Node node : fileNodeList) {
                                    if (node.getDisplayName().equals(nodeName)) {
                                        nodeDTOV2.setNodeId(node.getNodeId());
                                        nodeDTOV2.setNodeType(node.getNodeType());
                                        nodeDTOV2.setDisplayName(nodeName);
                                        nodeDTOV2.setNodeName(node.getNodeName());
                                        nodeDTOV2List.add(nodeDTOV2);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return nodeDTOV2List;
    }

    @Override
    public int add(GkStationConfiguration gkStationConfiguration) {
        if (gkStationConfiguration.getToUse() == null) {
            gkStationConfiguration.setToUse(1);
        }
        if (gkStationConfiguration.getResultConversion() == null) {
            gkStationConfiguration.setResultConversion(0);
        }
        gkStationConfiguration.setCreateAt(new Date());
        gkStationConfiguration.setLastModifyAt(new Date());
        if (gkStationConfiguration.getType() == 1) {
            GkStation gkStation = new GkStation();
            gkStation.setId(gkStationConfiguration.getStationId());
            List<GkStationVo> list = gkStationMapper.list(gkStation);
            for (GkStationVo gkStationVo : list) {
                if (gkStationVo.getConfigureType() == 3) {
                    gkStation.setConfigureType(2);
                    gkStationMapper.edit(gkStation);
                }
            }
        }
        return gkStationConfigurationMapper.add(gkStationConfiguration);
    }

    @Override
    public AjaxResult edit(GkStationConfiguration gkStationConfiguration) {
        int row = gkStationConfigurationMapper.edit(gkStationConfiguration);
        if (row > 0) {
            if (gkStationConfiguration.getType() == 1) {
                GkStation gkStation = new GkStation();
                gkStation.setId(gkStationConfiguration.getStationId());
                List<GkStationVo> list = gkStationMapper.list(gkStation);
                for (GkStationVo gkStationVo : list) {
                    if (gkStationVo.getConfigureType() == 3) {
                        gkStation.setConfigureType(2);
                        gkStationMapper.edit(gkStation);
                    }
                }
            }
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Transactional
    @Override
    public AjaxResult removeByIds(Long[] ids) {
        int row = gkStationConfigurationMapper.removeByIds(ids);
        // 删除变量相关保存的值
        gkStationConfigurationValuesMapper.deleteByStationConfigurationIds(Arrays.asList(ids));
        // 删除变量相关的事件
        gkStationEventMapper.removeByConfigurationIds(ids);
        if (row > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Transactional
    @Override
    public AjaxResult editList(GkStationConfiguration[] gkStationConfigurations) {
        List<Long> stationConfigurationIds = new ArrayList<>();
        if (gkStationConfigurations == null || gkStationConfigurations.length == 0) {
            return AjaxResult.error("无可保存的变量");
        }
        for (GkStationConfiguration gkStationConfiguration : gkStationConfigurations) {
            stationConfigurationIds.add(gkStationConfiguration.getId());
        }
        int flag = 0;
        List<GkStationConfiguration> gkStationConfigurationList = gkStationConfigurationMapper.selectInfoByIdsNotByUse(stationConfigurationIds);
        for (GkStationConfiguration gkStationConfiguration : gkStationConfigurationList) {
            for (GkStationConfiguration stationConfiguration : gkStationConfigurations) {
                if (gkStationConfiguration.getId().longValue() == stationConfiguration.getId().longValue()) {
                    if (!StringUtils.equals(gkStationConfiguration.getDefaultValue(), stationConfiguration.getSaveValue())) {
                        flag = 1;
                    }
                }
            }
        }
        if (flag == 1) {
            int row = gkStationConfigurationMapper.editList(gkStationConfigurations);
            if (row > 0) {
                if (gkStationConfigurations != null && gkStationConfigurations.length > 0) {
                    if (gkStationConfigurations[0].getType() == 1) {
                        GkStation gkStation = new GkStation();
                        gkStation.setId(gkStationConfigurations[0].getStationId());
                        gkStation.setConfigureType(2);
                        gkStationMapper.edit(gkStation);
                    }
                }
                return AjaxResult.success();
            }
            return AjaxResult.error();
        }
        return AjaxResult.success("内容无变化");
    }


    @Override
    public List<GkStationConfigurationVo> export(GkStationConfiguration gkStationConfiguration) {
        List<GkStationConfigurationVo> list = list(gkStationConfiguration);
        return list;
    }

    @Override
    public List<GkSparePartsVo> repairRecord(String barCode) {
        List<GkStationConfigurationValues> gkStationConfigurationValues = gkStationConfigurationValuesMapper.selectByValue(barCode);
        if (gkStationConfigurationValues == null) {
            return new ArrayList<>();
        }
        List<Long> collect = gkStationConfigurationValues.stream().map(GkStationConfigurationValues::getStationConfigurationConnectId).collect(Collectors.toList());
        Long[] longs = new Long[collect.size()];
        for (int i = 0; i < collect.size(); i++) {
            longs[i] = collect.get(i);
        }
        List<GkStationConfigurationConnect> configurationConnect = gkStationConfigurationConnectMapper.selectInfoByIds(longs);
        List<Long> collect1 = configurationConnect.stream().map(GkStationConfigurationConnect::getStationId).collect(Collectors.toList());
        // 根据工位id找到产品id
        List<GkStation> gkStations = gkStationMapper.selectInfosByIds(collect1);
        List<Long> productIds = gkStations.stream().map(GkStation::getProductId).distinct().collect(Collectors.toList());
        List<GkSparePartsVo> gkSparePartsVos = gkSparePartsMapper.selectInfoByProductIds(productIds);
        return gkSparePartsVos;
    }

    @Override
    public void exportRepairRecord(String barCode, HttpServletResponse response, HttpServletRequest request) {
        // 根据产品下面的所有工位id找到所有配置类型为2的配置变量信息
        if(ServletUtils.getParameter("pageSize")!=null&&ServletUtils.getParameter("pageNum")!=null){
            startPage();
        }
        List<GkStationConfigurationValues> gkStationConfigurationValues = gkStationConfigurationValuesMapper.selectByValue(barCode);
        long total = getDataTable(gkStationConfigurationValues).getTotal();
        if (gkStationConfigurationValues==null){
            return ;
        }
        List<Long> stationConfigurationConnectId = gkStationConfigurationValues.stream().map(GkStationConfigurationValues::getStationConfigurationConnectId).collect(Collectors.toList());
        Long[] longs = new Long[stationConfigurationConnectId.size()];
        for (int i = 0; i < stationConfigurationConnectId.size(); i++) {
            longs[i] = stationConfigurationConnectId.get(i);
        }
        if (longs.length==0){
            return ;
        }
        List<GkStationConfigurationConnect> configurationConnect = gkStationConfigurationConnectMapper.selectInfoByIds(longs);

        List<Long> configIds = gkStationConfigurationValues.stream().map(GkStationConfigurationValues::getStationConfigurationId).collect(Collectors.toList());
        List<GkStationConfiguration> gkStationConfigurationList = gkStationConfigurationMapper.selectInfoByIds(configIds);
        List<Long> stationIds = gkStationConfigurationList.stream().map(GkStationConfiguration::getStationId).distinct().collect(Collectors.toList());
        List<GkStation> gkStations = gkStationMapper.selectInfosByIds(stationIds);
        List<GkProductionLineVo> productionLineVos = gkProductionLineMapper.list(null);
        List<ReworkVo> reworkVos = new ArrayList<>();
        for (GkStationConfigurationValues gkStationConfigurationValue : gkStationConfigurationValues) {
            ReworkVo reworkVo = new ReworkVo();
            for (GkStationConfigurationConnect gkStationConfigurationConnect : configurationConnect) {
                if (gkStationConfigurationConnect.getId().longValue()==gkStationConfigurationValue.getStationConfigurationConnectId().longValue()) {
                    reworkVo.setProductName(gkStationConfigurationConnect.getProductName());
                }
            }
            reworkVo.setConfigValuesId(gkStationConfigurationValue.getId());
            GkStationConfiguration gkStationConfiguration1 = null;
            for (GkStationConfiguration gkStationConfiguration : gkStationConfigurationList) {
                if (gkStationConfiguration.getId().longValue()==gkStationConfigurationValue.getStationConfigurationId().longValue()) {
                    gkStationConfiguration1 = gkStationConfiguration;
                    break;
                }
            }
            if (gkStationConfiguration1!=null) {
                for (GkStation gkStation : gkStations) {
                    if (gkStationConfiguration1.getStationId().longValue() == gkStation.getId().longValue()) {
                        reworkVo.setStationId(gkStation.getId());
                        for (GkProductionLineVo productionLineVo : productionLineVos) {
                            if (productionLineVo.getId().longValue()==gkStation.getProductionLineId().longValue()) {
                                reworkVo.setStationName(productionLineVo.getName()+"："+gkStation.getName());
                            }
                        }
                        if (StringUtils.isEmpty(reworkVo.getStationName())){
                            reworkVo.setStationName(gkStation.getName());
                        }
                    }
                }
            }
            reworkVo.setSparePartsCode(gkStationConfigurationValue.getValue());
            reworkVo.setCreateAt(gkStationConfigurationValue.getCreateAt());
            reworkVos.add(reworkVo);
        }
        ExcelUtil<ReworkVo> gkSparePartsVoExcelUtil = new ExcelUtil<>(ReworkVo.class);
        gkSparePartsVoExcelUtil.exportExcel(response, reworkVos, "返修记录表");
    }


    @Override
    public AjaxResult setParameters(GkStationConfiguration[] gkStationConfigurations, Long templateId) {
        Map<String, Object> map = new HashMap<>();
        // todo 工位设置  后端通讯  定位todo
        if (gkStationConfigurations == null || gkStationConfigurations.length <= 0) {
            return AjaxResult.error("传入值为空");
        }
        List<Long> stationIds1 = Arrays.stream(gkStationConfigurations).map(GkStationConfiguration::getStationId).collect(Collectors.toList());
        List<GkStation> gkStations1 = gkStationMapper.selectInfosByIds(stationIds1);
        if (!BarCodeTool.inspect(Arrays.asList(gkStationConfigurations),gkStations1)) {
            return AjaxResult.error(BarCodeTool.msg);
        }
        OpcUaWriteReqV2 opcUaWriteReqV2 = new OpcUaWriteReqV2();
        List<Long> stationIds = Arrays.stream(gkStationConfigurations).map(GkStationConfiguration::getStationId).collect(Collectors.toList());
        List<GkStation> gkStations = gkStationMapper.selectInfosByIds(stationIds);
        List<Long> signal = gkStations.stream().map(GkStation::getSignalCommunicationId).distinct().collect(Collectors.toList());
        List<Long> productLineIds = gkStations.stream().map(GkStation::getProductionLineId).distinct().collect(Collectors.toList());
        List<GkProductionLineVo> gkProductionLineVos = gkProductionLineMapper.selectInfoByIds(productLineIds);
        List<GkSignalCommunication> gkSignalCommunications = gkSignalCommunicationMapper.selectInfoByIds(signal);
        if (gkSignalCommunications != null && gkSignalCommunications.size() > 0) {
            for (GkSignalCommunication gkSignalCommunication : gkSignalCommunications) {
                if (gkSignalCommunication.getCommunicationProtocol() != null) {
                    opcUaWriteReqV2.setUid(gkSignalCommunication.getCommunicationProtocol());
                }
            }
        }
        if (StringUtils.isEmpty(opcUaWriteReqV2.getUid())){
            return AjaxResult.error("连接异常");
        }
        List<Node> fileNodeList = new ArrayList<>();
        OpcUaList opcUaList = ReadOpcUaNodeInfoTool.GetOpcUaList();
        List<OpcUa> uaList = opcUaList.getOpcUaList();
        for (OpcUa opcUa : uaList) {
            if (opcUa.getUid().equals(opcUaWriteReqV2.getUid())) {
                fileNodeList = opcUa.getNodeList();
            }
        }
        List<NodeDTOV2> nodeDTOV2List = new ArrayList<>();
        String productName = "";
        for (GkStationConfiguration gkStationConfiguration : gkStationConfigurations) {
            if (gkStationConfiguration.getVariableName().equals("ProductType")) {
                productName = gkStationConfiguration.getSaveValue();
            }
            if (gkStationConfiguration.getDataType().equals("boolean")) {
                gkStationConfiguration.setDataType("bool");
            }
            gkStationConfiguration.setDataType(gkStationConfiguration.getDataType().toUpperCase());
            NodeDTOV2 nodeDTOV2 = new NodeDTOV2();
            String nodeName = new String();
            for (GkStation gkStation : gkStations) {
                if (gkStation.getId().longValue() == gkStationConfiguration.getStationId().longValue()) {
                    if (gkStationConfiguration.getType() == 1) {
                        nodeName = "DB_RECIPE." + ((gkStation.getCode() == null || gkStation.getCode().equals("")) ? "" : (gkStation.getCode() + ".")) + gkStationConfiguration.getVariableName();
                    }
                    if (gkStationConfiguration.getType() == 2) {
                        for (GkProductionLineVo gkProductionLineVo : gkProductionLineVos) {
                            if (gkStation.getProductionLineId().longValue() == gkProductionLineVo.getId().longValue()) {
                                nodeName = gkProductionLineVo.getCode() + "." + ((gkStation.getCode() == null || gkStation.getCode().equals("")) ? "" : (gkStation.getCode() + ".")) + gkStationConfiguration.getVariableName();
                            }
                        }
                    }
                    if (gkStationConfiguration.getType() == 3) {
                        nodeName = "data_pc_plc.pc_reply." + gkStationConfiguration.getVariableName();
                    }
                    if (gkStationConfiguration.getType() == 4) {
                        nodeName = "data_pc_plc.plc_request." + gkStationConfiguration.getVariableName();
                    }
                }
            }
            nodeDTOV2.setNodeName(nodeName);
            nodeDTOV2.setNodeType(gkStationConfiguration.getDataType());
            Object obj = new Object();
            if (gkStationConfiguration.getDataType().equals("BOOL")) {
                if (StringUtils.equals(gkStationConfiguration.getSaveValue(), "true")) {
                    obj = Boolean.TRUE;
                } else {
                    obj = Boolean.FALSE;
                }
            } else if (gkStationConfiguration.getDataType().equals("STRING")) {
                if (StringUtils.isEmpty(gkStationConfiguration.getSaveValue())) {
                    obj = null;
                } else {
                    obj = gkStationConfiguration.getSaveValue();
                }
            } else if (StringUtils.isEmpty(gkStationConfiguration.getSaveValue())) {
                obj = null;
            } else {
                Pattern pattern = Pattern.compile("^[-+]?[0-9]+(\\.[0-9]+)?$");
                Matcher isNum = pattern.matcher(gkStationConfiguration.getSaveValue());
                if (!isNum.matches()) {
                    return AjaxResult.error(gkStationConfiguration.getVariableName() + "输入值类型不匹配");
                }
                if (gkStationConfiguration.getDataType().equals("FLOAT")) {
                    obj = Float.parseFloat(gkStationConfiguration.getSaveValue());
                } else {
                    if (gkStationConfiguration.getSaveValue().indexOf('.') > 0) {
                        return AjaxResult.error(gkStationConfiguration.getVariableName() + "输入值类型不匹配");
                    }
                    obj = Integer.parseInt(gkStationConfiguration.getSaveValue());
                }
            }
            map.put(nodeName, obj);
            nodeDTOV2.setNodeValue(obj);
            if (CollUtil.isNotEmpty(fileNodeList)){
                for (Node node : fileNodeList) {
                    if (nodeName.equals(node.getDisplayName())) {
                        nodeDTOV2.setNodeId(node.getNodeId());
                        break;
                    }
                }
            }
            nodeDTOV2List.add(nodeDTOV2);
            log.info("设置属性：{}", nodeDTOV2.toString());
        }
        opcUaWriteReqV2.setWriteNodeList(nodeDTOV2List);
        Ret<List<NodeDTOV2>> listRet = null;
        String s = null;
        try {
            if (communication.equals("socket")) {
                BufferedWriter bufferedWriter = ScheduledTask.bufferedWriter;
                BufferedReader bufferedReader = ScheduledTask.bufferedReader;
                SocketDataVo socketDataVo = new SocketDataVo();
                socketDataVo.setUid(opcUaWriteReqV2.getUid());
                List<ServiceData> nodeList = new ArrayList<>();
                for (NodeDTOV2 nodeDTOV2 : nodeDTOV2List) {
                    ServiceData serviceData = new ServiceData();
                    serviceData.setNodeName(nodeDTOV2.getNodeName());
                    serviceData.setDisplayName(nodeDTOV2.getNodeName());
                    serviceData.setNodeValue(nodeDTOV2.getNodeValue());
                    serviceData.setNodeType(nodeDTOV2.getNodeType());
                    nodeList.add(serviceData);
                }
                socketDataVo.setNodeList(nodeList);
                bufferedWriter.write("setData?" + JSON.toJSONString(socketDataVo) + "\n");
                bufferedWriter.flush();
                s = bufferedReader.readLine();
            } else if (communication.equals("plc")) {
                log.info("设置的uuid：{}", opcUaWriteReqV2.getUid());
                if (!OpcUATool.isConnect(opcUaServiceV2, opcUaWriteReqV2)) {
                    int i = 0;
                    for (i=0 ; i < 10; i++) {
                        if (OpcUATool.isConnect(opcUaServiceV2,opcUaWriteReqV2)){
                            break;
                        }
                        Thread.sleep(200);
                    }
                    if (i==10) {
                        return AjaxResult.error("plc连接异常,请重新下发参数");
                    }
                }
                opcUaWriteReqV2 = OpcUATool.valueIsChange(opcUaServiceV2,opcUaWriteReqV2);
                for (int i = 0; i < 10; i++) {
                    listRet = opcUaServiceV2.writeData(opcUaWriteReqV2);
                    if (listRet!=null){
                        break;
                    }
                }
            }
        } catch (Exception e) {
            log.error("opcUaServiceV2.writeData(opcUaWriteReqV2)出错");
        }
        // todo 工位设置 保存参数下发记录
        Map<Long, List<GkStation>> listMap = gkStations.parallelStream().collect(Collectors.groupingBy(GkStation::getProductionLineId));
        GkSettingLog gkSettingLog = new GkSettingLog();
        String setStation = new String();
        for (GkProductionLineVo gkProductionLineVo : gkProductionLineVos) {
            List<GkStation> gkStations2 = listMap.get(gkProductionLineVo.getId());
            if (CollUtil.isNotEmpty(gkStations2)) {
                List<String> stationNames = gkStations2.stream().filter(gk -> StringUtils.isNotEmpty(gk.getName())).map(GkStation::getName).collect(Collectors.toList());
                String join = StringUtils.join(stationNames, ",");
                setStation += gkProductionLineVo.getName()+":"+"("+join+");";
            }
        }
        gkSettingLog.setProductName(productName);
        gkSettingLog.setSetStation(setStation);
        gkSettingLogService.add(gkSettingLog);
//        if (StringUtils.isNotEmpty(productName)) {
//            List<GkReservedInfo> gkReservedInfoList = new ArrayList<>();
//            GkReservedInfo gkReservedInfo = new GkReservedInfo();
//            gkReservedInfo.setId(1l);
//            gkReservedInfo.setValue(productName);
//            gkReservedInfoList.add(gkReservedInfo);
//            GkProduct gkProduct = new GkProduct();
//            gkProduct.setTemplateName(productName);
//            List<GkProduct> list = gkProductMapper.list(gkProduct);
//            GkReservedInfo gkReservedInfo1 = new GkReservedInfo();
//            if (CollUtil.isNotEmpty(list)){
//                for (GkProduct product : list) {
//                    gkReservedInfo1.setValue(product.getCode());
//                    gkReservedInfo1.setId(2l);
//                }
//            }
//            gkReservedInfoList.add(gkReservedInfo1);
//            int row = gkReservedInfoMapper.editList(gkReservedInfoList);
//        }
        if (listRet == null && s == null) {
            log.info("setParameters连接为空");
            return AjaxResult.error("下发参数失败,请重新下发参数");
        }
        if (s == null && listRet.getData() == null) {
            log.info("setParameters值为空");
            return AjaxResult.error("下发参数失败,请重新下发参数");
        }
        List<GkStationConfiguration> gkStationConfigurationList = new ArrayList<>();
        List<NodeDTOV2> data = new ArrayList<>();
        if (communication.equals("socket")) {
            SocketDataVo socketDataVo = JSON.parseObject(s, SocketDataVo.class);
            List<ServiceData> nodeList = socketDataVo.getNodeList();
            for (ServiceData serviceData : nodeList) {
                NodeDTOV2 nodeDTOV2 = new NodeDTOV2();
                nodeDTOV2.setNodeValue(serviceData.getNodeValue());
                nodeDTOV2.setNodeType(serviceData.getNodeType());
                nodeDTOV2.setNodeName(serviceData.getNodeName());
                nodeDTOV2.setNodeId(serviceData.getNodeId());
                nodeDTOV2.setDisplayName(serviceData.getDisplayName());
                nodeDTOV2.setStatusCode(0l);
                data.add(nodeDTOV2);
            }
        } else if (communication.equals("plc")) {
            data = listRet.getData();
        }
        for (NodeDTOV2 datum : data) {
            if (datum.getStatusCode().intValue() != 0) {
                for (GkStationConfiguration gkStationConfiguration : gkStationConfigurations) {
                    String nodeName = new String();
                    for (GkStation gkStation : gkStations) {
                        if (gkStation.getId().longValue() == gkStationConfiguration.getStationId().longValue()) {
                            if (gkStationConfiguration.getType() == 1) {
                                nodeName = "DB_RECIPE." + ((gkStation.getCode() == null || gkStation.getCode().equals("")) ? "" : (gkStation.getCode() + ".")) + gkStationConfiguration.getVariableName();
                            }
                            if (gkStationConfiguration.getType() == 2) {
                                for (GkProductionLineVo gkProductionLineVo : gkProductionLineVos) {
                                    if (gkStation.getProductionLineId().longValue() == gkProductionLineVo.getId().longValue()) {
                                        nodeName = gkProductionLineVo.getCode() + "." + ((gkStation.getCode() == null || gkStation.getCode().equals("")) ? "" : (gkStation.getCode() + ".")) + gkStationConfiguration.getVariableName();
                                    }
                                }
                            }
                            if (gkStationConfiguration.getType() == 3) {
                                nodeName = "data_pc_plc.pc_reply." + gkStationConfiguration.getVariableName();
                            }
                            if (gkStationConfiguration.getType() == 4) {
                                nodeName = "data_pc_plc.plc_request." + gkStationConfiguration.getVariableName();
                            }
                        }
                    }
                    if (datum.getNodeName().equals(nodeName)) {
                        // 修改失败则不修改默认值
                        gkStationConfiguration.setSaveValue(null);
                        gkStationConfiguration.setDataType(null);
                        gkStationConfiguration.setStatusMsg(datum.getStatusMsg());
                        gkStationConfigurationList.add(gkStationConfiguration);
                    }
                }
            }
        }

        if (templateId!=null){
            gkStationConfigureTemplateInfoMapper.editList(templateId, Arrays.asList(gkStationConfigurations));
        }
        // 设置以后 修改默认值
        gkStationConfigurationMapper.editList(gkStationConfigurations);
        GkStation gkStation = new GkStation();
        gkStation.setId(gkStationConfigurations[0].getStationId());
        if (gkStationConfigurationList.size() > 0) {
            // 部分设置
            gkStation.setConfigureType(StationDict.PARTIALSETTINGS.value);
        } else {
            // 设置成功
            gkStation.setConfigureType(StationDict.CONFIGURED.value);
        }
        // 修改工位状态
        gkStationMapper.edit(gkStation);
        // todo 下发验证定位


        if (gkStationConfigurationList.size() > 0) {
            return AjaxResult.success("有未设置成功参数", gkStationConfigurationList);
        }
        if (communication.equals("plc")) {
            try {
                // todo 设置延时定位
                Thread.sleep(12000);
                OpcUaReadReqV2 opcUaReadReqV2 = new OpcUaReadReqV2();
                opcUaReadReqV2.setUid(opcUaWriteReqV2.getUid());
                Ret<BatchReadDTO> batchReadDTORet = opcUaServiceV2.readData(opcUaReadReqV2);
                if (batchReadDTORet != null) {
                    BatchReadDTO data1 = batchReadDTORet.getData();
                    if (data1 != null && data1.getIsConn()) {
                        List<NodeDTOV2> writeNodeList = opcUaWriteReqV2.getWriteNodeList();
                        for (NodeDTOV2 nodeDTOV2 : data1.getDtoList()) {
                            for (NodeDTOV2 dtov2 : writeNodeList) {
                                if (nodeDTOV2.getDisplayName().equals(dtov2.getNodeName())) {
                                    log.info("需要修改的变量：{}-》{}", dtov2.getNodeName(), dtov2.getNodeValue());
                                    log.info("修改后的变量：{}-》{}", nodeDTOV2.getDisplayName(), nodeDTOV2.getNodeValue());
                                }
                            }
                        }
                    }
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult listByProductLineId(Long productLineId) {
        GkStation gkStation = new GkStation();
        gkStation.setProductionLineId(productLineId);
        List<GkStationVo> list = gkStationMapper.list(gkStation).stream().sorted(Comparator.comparing(GkStationVo::getDefaultStation).reversed()).collect(Collectors.toList());
        List<Long> stationIds = list.stream().map(GkStationVo::getId).collect(Collectors.toList());
        if (stationIds == null || stationIds.size() == 0) {
            return AjaxResult.success("暂无工位");
        }
        List<GkStationConfiguration> gkStationConfigurations = gkStationConfigurationMapper.selectInfoByType1Stations(stationIds);
        List<GkStationConfigurationVo> gkStationConfigurationVos = new ArrayList<>();
        OpcUaReadReqV2 opcUaReadReqV2 = new OpcUaReadReqV2();
        List<Long> collect = list.stream().map(GkStationVo::getSignalCommunicationId).distinct().collect(Collectors.toList());
        List<GkSignalCommunication> gkSignalCommunications = gkSignalCommunicationMapper.selectInfoByIds(collect);
        if (gkSignalCommunications != null && gkSignalCommunications.size() > 0) {
            for (GkSignalCommunication gkSignalCommunication : gkSignalCommunications) {
                if (gkSignalCommunication.getCommunicationProtocol() != null) {
                    opcUaReadReqV2.setUid(gkSignalCommunication.getCommunicationProtocol());
                }
            }
        }
        List<GkProductionLineVo> gkProductionLineVos = gkProductionLineMapper.selectInfoByIds(Collections.singletonList(productLineId));
        // todo 返回实时值 定位todo
        BufferedReader bufferedReader = ScheduledTask.bufferedReader;
        BufferedWriter bufferedWriter = ScheduledTask.bufferedWriter;
        try {
            List<SocketDataVo> socketDataVos = new ArrayList<>();
            BatchReadDTO listRet = null;
            try {
                if (communication.equals("socket")) {
                    bufferedWriter.write(opcUaReadReqV2.getUid() + "\n");
                    bufferedWriter.flush();
                    String s = bufferedReader.readLine();
                    JSONArray objects = JSON.parseArray(s);
                    for (Object object : objects) {
                        SocketDataVo socketDataVo = JSON.parseObject(object.toString(), SocketDataVo.class);
                        socketDataVos.add(socketDataVo);
                    }
                } else if (communication.equals("plc")) {
                    ReadByNodeReq readByNodeReq = new ReadByNodeReq();
                    readByNodeReq.setUid(opcUaReadReqV2.getUid());
                    List<GkStation> gkStations = gkStationMapper.selectInfosByIds(stationIds);
                    List<NodeDTOV2> nodeDTOV2List = GetNodeDTO2List(null, gkStationConfigurations, gkStations, gkProductionLineVos, opcUaReadReqV2.getUid());
//                    log.info("查询参数匹配id：{}",nodeDTOV2List);
                    readByNodeReq.setNodeDTOV2List(nodeDTOV2List);
                    Ret<BatchReadDTO> batchReadDTORet = opcUaServiceV2.readDataByNodeList(readByNodeReq);
                    listRet = batchReadDTORet.getData();
//                    listRet = opcUaServiceV2.readData(opcUaReadReqV2).getData();
                }
            } catch (Exception e) {
                log.info("listByProductLineId读取连接异常,:{}",e.getMessage());
            }

            List<NodeDTOV2> data = new ArrayList<>();
            if (communication.equals("socket")) {
                for (SocketDataVo socketDataVo : socketDataVos) {
                    List<ServiceData> nodeList = socketDataVo.getNodeList();
                    for (ServiceData serviceData : nodeList) {
                        NodeDTOV2 nodeDTOV2 = new NodeDTOV2();
                        nodeDTOV2.setNodeValue(serviceData.getNodeValue());
                        nodeDTOV2.setNodeType(serviceData.getNodeType());
                        nodeDTOV2.setNodeName(serviceData.getNodeName());
                        nodeDTOV2.setNodeId(serviceData.getNodeId());
                        nodeDTOV2.setDisplayName(serviceData.getDisplayName());
                        nodeDTOV2.setStatusCode(0l);
                        data.add(nodeDTOV2);
                    }
                }
            } else if (communication.equals("plc")) {
                if (listRet != null && listRet.getIsConn()) {
                    data = listRet.getDtoList();
                }
            }
            for (GkStationConfiguration gkStationConfiguration : gkStationConfigurations) {
                GkStationConfigurationVo gkStationConfigurationVo = new GkStationConfigurationVo();
                gkStationConfigurationVo.setStationId(gkStationConfiguration.getStationId());
                gkStationConfigurationVo.setDataType(gkStationConfiguration.getDataType());
                gkStationConfigurationVo.setId(gkStationConfiguration.getId());
                gkStationConfigurationVo.setType(gkStationConfiguration.getType());
                gkStationConfigurationVo.setCheckLength(gkStationConfiguration.getCheckLength());
                gkStationConfigurationVo.setCreateAt(gkStationConfiguration.getCreateAt());
                gkStationConfigurationVo.setDisplayName(gkStationConfiguration.getDisplayName());
                gkStationConfigurationVo.setNameSpaces(gkStationConfiguration.getNameSpaces());
                gkStationConfigurationVo.setToUse(gkStationConfiguration.getToUse());
                gkStationConfigurationVo.setVariableAddress(gkStationConfiguration.getVariableAddress());
                gkStationConfigurationVo.setVariableComments(gkStationConfiguration.getVariableComments());
                gkStationConfigurationVo.setVariableIndex(gkStationConfiguration.getVariableIndex());
                gkStationConfigurationVo.setVariableName(gkStationConfiguration.getVariableName());
                gkStationConfigurationVo.setVariableRemarks(gkStationConfiguration.getVariableRemarks());
                for (NodeDTOV2 datum : data) {
                    Object obj = new Object();
                    if (datum.getNodeValue() != null) {
                        int i = datum.getNodeValue().toString().replace("}", "").indexOf("=");
                        if (i > 0) {
                            obj = datum.getNodeValue().toString().replace("}", "").substring(i + 1);
                        } else {
                            obj = datum.getNodeValue();
                        }
                    } else {
                        obj = datum.getNodeValue();
                    }
                    for (GkStationVo gkStationVo : list) {
                        if (gkStationVo.getId().longValue() == gkStationConfiguration.getStationId().longValue()) {
                            gkStation.setCode(gkStationVo.getCode());
                        }
                    }
                    String nodeName = new String();
                    if (gkStationConfiguration.getType() == 1) {
                        nodeName = "DB_RECIPE." + ((gkStation.getCode() == null || gkStation.getCode().equals("")) ? "" : (gkStation.getCode() + ".")) + gkStationConfiguration.getVariableName();
                    }
                    if (gkStationConfiguration.getType() == 2) {
                        for (GkProductionLineVo gkProductionLineVo : gkProductionLineVos) {
                            if (gkStation.getProductionLineId().longValue() == gkProductionLineVo.getId().longValue()) {
                                nodeName = gkProductionLineVo.getCode() + "." + ((gkStation.getCode() == null || gkStation.getCode().equals("")) ? "" : (gkStation.getCode() + ".")) + gkStationConfiguration.getVariableName();
                            }
                        }
                    }
                    if (gkStationConfiguration.getType() == 3) {
                        // 写入交互变量类型  3写
                        nodeName = "data_pc_plc.pc_reply." + gkStationConfiguration.getVariableName();
                    }
                    if (gkStationConfiguration.getType() == 4) {
                        // 读取交互变量类型  4读
                        nodeName = "data_pc_plc.plc_request." + gkStationConfiguration.getVariableName();
                    }
                    if (datum.getDisplayName().equals(nodeName)) {
                        gkStationConfigurationVo.setDefaultValue(obj);
                    }
                }
                Object newobj = new Object();
                if (gkStationConfiguration.getDataType().equals("boolean")) {
                    if (gkStationConfiguration.getDefaultValue() == null || gkStationConfiguration.getDefaultValue().equals("false")) {
                        newobj = Boolean.FALSE;
                    } else {
                        newobj = Boolean.TRUE;
                    }
                } else {
                    newobj = gkStationConfiguration.getDefaultValue() == null ? "" : gkStationConfiguration.getDefaultValue();
                }
                gkStationConfigurationVo.setSaveValue(newobj);
                gkStationConfigurationVos.add(gkStationConfigurationVo);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Map<Long, List<GkStationConfigurationVo>> listMap = gkStationConfigurationVos.parallelStream().collect(Collectors.groupingBy(GkStationConfigurationVo::getStationId));
        List<Map<String, Object>> mapList = new ArrayList<>();
        int count = 0;
        for (GkStationVo gkStationVo : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("stationId", gkStationVo.getId());
            map.put("stationName", gkStationVo.getName());
            map.put("stationCode", gkStationVo.getCode());
            map.put("configureType", gkStationVo.getConfigureType());
            map.put("stationConfigurations", listMap.size() > 0 ? listMap.get(gkStationVo.getId()) : new ArrayList<>());
            if (gkStationVo.getConfigureType() != 3) {
                count += 1;
            }
            mapList.add(map);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("noConfigurationNum", count);
        map.put("stationInfos", mapList);
        return AjaxResult.success(map);
    }


}
